<template>
    <div class="my-voucher background" :style="{height: `${screenHeight}px`}">
        <mt-loadmore
        v-if="listData.length>0"
        ref="loadmore"
        :bottom-method="loadBottom" 
        :bottomDistance="10"
        bottomDropText=""
        :autoFill="false"
        bottomLoadingText=""
        :bottom-all-loaded="allLoaded">
            <div class="my-voucher-list" @click="gotoVoucherInfo(item.registrationId)" v-for="item in listData" :key="item.id">
                <div class="my-voucher-list-left">
                    <h4>{{item.registration.title}}</h4>
                </div>
                <div class="my-voucher-list-right"> ></div>
            </div>
            <span style="font-size:12px;color:#bbb;padding: 2px;" v-if="!allLoaded">上拉加载更多↑</span>
            <span style="font-size:12px;color:#bbb;padding: 2px;" v-if="allLoaded && listData.length >10">—— 你触及到我的底线啦~ ——</span>
        </mt-loadmore>
        <span style="font-size:12px;color:#bbb;padding: 2px;" v-else>没有数据~</span>
    </div>
</template>

<script>
    import {getParticipationDate} from '../../api/request.js'
    import { setStore, getStore } from '../../assets/js/utils.js'
    import { Loadmore } from 'mint-ui';
    export default {
        name: "Voucher",
        data() {
            return {
                allLoaded:false,
                userId:'',
                pageNo:1,
                listData:[]
            }
        },
        computed: {
            screenHeight() {
                return window.innerHeight-60;
            }
        },
        created(){
            let userId = getStore('userId');
            let user = getStore('user');
            if(userId && user){
                this.userId = userId;
                this.loadBottom();
            }else{
                var fhUrl = window.location.href;
                setStore('fhUrl', fhUrl);
                this.$router.push('/wxLogin')
            }
            this.$store.commit('UPDATE_PAGE_TITLE', '凭证列表');
            this.$store.commit('UPDATE_BACKSHOW', false);
            this.$store.commit('UPDATE_FOOTER', false);
        },
        mounted() {},
        methods: {
            gotoVoucherInfo(id) {
                this.$router.push({name:'VoucherInfo',params:{id:id}})
            },
            loadBottom() {
                let that = this;
                getParticipationDate(this.userId,this.pageNo).then((res) => {
                    if (res.code == 1) {
                        that.pageNo++;
                        that.listData.push.apply(that.listData,res.data); 
                        if(res.data.length<10){
                            that.allLoaded = true; // 数据已全部获取完毕
                        }
                    }
                    that.$refs.loadmore.onBottomLoaded();
                }).catch(err => console.log(err)) 
            },
        },
        components:{
            "mt-loadmore":Loadmore
        }
    }
</script>

<style scoped lang="scss">
    .my-voucher{
        margin-top: 40px;
        padding: 10px;
        &-list{
            box-sizing: border-box;
            overflow: auto;
            background-color: #ffffff;
            width: 100%;
            margin: 5px auto;
            padding: 15px;
            border-radius: 10px;
            text-align: left;
            &-left{
                float: left;
            }
            &-right{
                float: right;
            }
        }
    }
</style>